package pri.jz.keeper.ui.login

import android.content.Intent
import androidx.activity.viewModels
import androidx.compose.runtime.Composable
import pri.jz.keeper.ui.content.ContentActivity
import pri.jz.keeper.ui.register.RegisterActivity
import pri.jz.keeper.ui.base.BaseActivity
import pri.jz.keeper.ui.base.BaseViewModel
import pri.jz.keeper.ui.view.LoginView

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:登录
 */
class LoginActivity : BaseActivity() {
    private val loginViewModel: LoginViewModel by viewModels()
    override fun getViewModel(): BaseViewModel = loginViewModel
    @Composable
    override fun OnCreateView() {
        LoginView()
    }

    override suspend fun onObserver() {
        loginViewModel.loginState.collect {
            when(it) {
                is LoginState.SUCCESS -> loginSuccess(it.id)
                is LoginState.REGISTER -> register()
                is LoginState.DEFAULT -> {}
            }
        }
    }


    private fun register() {
        startActivity(Intent(this, RegisterActivity::class.java))
    }

    private fun loginSuccess(id: Int) {
        startActivity(
            Intent(this, ContentActivity::class.java).apply {
                putExtra("userId",id)
            }
        )
        finish()
    }


}